package SwordForOffer.Problem20;

public class MyPrintMatrixInCircle {
    public void printMatrixInCircle(int[][] array) {
        if (array == null){
            return;
        }

        int n = 0;
        printMatrix(array,n);

    }

    private void printMatrix(int[][] array, int n) {
        if (array == null){
            return;
        }
        int rowEnd = array.length -1  - n;
        int columnEnd = array[0].length - 1 - n;
        int rowLength = array.length - 2 * n;
        int columnLength = array[0].length - 2 * n;
        if (rowLength <= 0 || columnLength <= 0){
            return;
        }

        if (rowLength == 1 && columnLength == 1){
            System.out.println(array[n][n]);
        }else if (rowLength > 1 && columnLength > 1){
            for (int i = n;i < columnEnd ;i++){
                System.out.println(array[n][i]);
            }
            for (int i = n;i < rowEnd ;i++){
                System.out.println(array[i][columnEnd]);
            }
            for (int i = columnEnd;i > n ;i--){
                System.out.println(array[rowEnd][i]);
            }
            for (int i = rowEnd;i > n ;i--){
                System.out.println(array[i][n]);
            }
        }else if (rowLength > 1){
            for (int i = n;i <= rowEnd ;i++){
                System.out.println(array[i][n]);
            }
        }else {
            for (int i = n;i <= columnEnd ;i++){
                System.out.println(array[n][i]);
            }
        }



        printMatrix(array,++n);

    }
}
